﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using PocketApplicationsMVC.Managers;
using System.Threading;
using System.Diagnostics;

namespace PocketApplicationsMVC.Views
{
    public class BoxNetController : IController
    {
        private IBoxNetView view;
        private PocketLibrary.Storage.BoxNetAPI bxApi;

        #region IController Members

        public IView View
        {
            get
            {
                return view;
            }
            set
            {
                view = (IBoxNetView)value;
                AttachView(view);
            }
        }

        #endregion

        public BoxNetController(IBoxNetView view)
        {
            bxApi = new PocketLibrary.Storage.BoxNetAPI();

            this.view = view;
            
            this.AttachView(this.view);
        }

        private void AttachView(IBoxNetView iMenuView)
        {
            // hook up to view events

            //view.FileProgress += new PocketLibrary.UploadProgressHandler(view_FileProgress);
            view.DownloadFile += new EventHandler<BoxNetDownloadArgs>(view_DownloadFileEvent);
            view.OnUserLogin += new EventHandler<BoxNetUserArgs>(view_OnUserLogin);
            view.UploadFile += new EventHandler(view_OnUploadFile);
            view.ListFolder += new EventHandler<BoxNetFileArgs>(view_OnListFolder);
            view.ShowMenuForm += new EventHandler(view_ShowMenuForm);
            view.UpdateFolder += new EventHandler(view_UpdateFolder);
            view.ListParentFolder += new EventHandler(view_ListParentFolder);

            // hook up to model events
            bxApi.OnTreeResponse += new PocketLibrary.Storage.BoxNetApiTreeHandler(bxApi_OnTreeResponse);
            bxApi.OnLogin += new PocketLibrary.Storage.BoxNetApiResponseHandler(bxApi_OnLogin);
            bxApi.OnUploadProgress += new PocketLibrary.UploadProgressHandler(bxApi_OnUploadProgress);
            bxApi.OnDownloadProgress += new PocketLibrary.UploadProgressHandler(bxApi_OnDownloadProgress);
            // handle both upload and download completed
            bxApi.OnDownload += new PocketLibrary.Storage.BoxNetApiResponseHandler(bxApi_OnDownload);

        }


        void view_ListParentFolder(object sender, EventArgs e)
        {
            view.UpdateStatus("Listing contents");
            bxApi.ListParentFolder();
            view.UpdateStatus("");
        }

        void view_UpdateFolder(object sender, EventArgs e)
        {
            view.UpdateStatus("Listing contents");
            bxApi.ListFolder();
            view.UpdateStatus("");
        }

        void view_ShowMenuForm(object sender, EventArgs e)
        {
            ApplicationManager.Instance.ShowMenuForm();
            view.Hide();
        }

        void view_OnListFolder(object sender, BoxNetFileArgs e)
        {
            //throw new NotImplementedException();
            view.UpdateStatus("Listing contents");
            bxApi.ListFolder(e.FileId);
            view.UpdateStatus("");
        }

        void view_OnUploadFile(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            view.UpdateStatus("Uploading");
            bxApi.Upload(view.FileToUpload);
        }

        void view_OnUserLogin(object sender, BoxNetUserArgs e)
        {
            view.UpdateStatus("Login");
            bxApi.Username = e.Username;
            bxApi.Password = e.Password;
            //bxApi.Login();
            ThreadPool.QueueUserWorkItem(new WaitCallback(this.boxNetLogin));
            //if (bxApi.Login())
            //{
            //    view.LoggedIn(true);
            //    bxApi.ListFolder(); 
            //}
            //else
            //    view.LoggedIn(false);
        }

        void boxNetLogin(object stateInfo)
        {
            bxApi.Login();
        }

        void view_DownloadFileEvent(object sender, BoxNetDownloadArgs e)
        {
            view.UpdateStatus("Downloading file");
            bxApi.Download(e.FileId, e.Target);
        }

        void view_FolderListUpdated(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }

        void view_Exit(object sender, EventArgs e)
        {
            Application.Exit();
        }

        void bxApi_OnLogin(object sender, PocketLibrary.Storage.BoxNetApiResponseArgs args)
        {
            //Debug.WriteLine(args.Response);
            if (args.Response == "logged")
            {
                view.UpdateStatus("Login success");
                view.LoggedIn(true);

                view.UpdateStatus("Listing contents");
                bxApi.ListFolder();

            }
            else
            {
                view.LoggedIn(false);
                //view.NotifyStatus("Login", "Login failed", false);
                view.UpdateStatus("Login failed");
            }
        }

        void bxApi_OnTreeResponse(object sender, PocketLibrary.Storage.BoxNetApiTreeArgs args)
        {
            //throw new NotImplementedException();
            view.UpdateFolderContents(args.TreeItems);
            view.UpdateStatus("Listing complete");
        }

        void bxApi_OnUploadProgress(object sender, PocketLibrary.UploadProgressEventArgs e)
        {
            view.UpdateFileProgress(e.CurrentProgress);
        }

        void bxApi_OnDownloadProgress(object sender, PocketLibrary.UploadProgressEventArgs e)
        {
            view.UpdateFileProgress(e.CurrentProgress);
        }

        void bxApi_OnDownload(object sender, PocketLibrary.Storage.BoxNetApiResponseArgs args)
        {
            view.UpdateStatus("File downloaded");
        }


        #region threading
        AutoResetEvent doneEvent;



        #endregion


    }
}
